resize.js ➔ initResizable   F
last analyzed

Complexity

Conditions 25

Size

Total Lines 112
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 80
c 0
b 0
f 0
dl 0
loc 112
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like resize.js ➔ initResizable often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
function initResizable()
2
{
3
  var cookie_namespace = 'doxygen';
4
  var sidenav,navtree,content,header,collapsed,collapsedWidth=0,barWidth=6,desktop_vp=768,titleHeight;
0 ignored issues
show
Unused Code introduced by
The variable titleHeight seems to be never used. Consider removing it.
Loading history...
5
6
  function readCookie(cookie)
7
  {
8
    var myCookie = cookie_namespace+"_"+cookie+"=";
9
    if (document.cookie) {
10
      var index = document.cookie.indexOf(myCookie);
11
      if (index != -1) {
12
        var valStart = index + myCookie.length;
13
        var valEnd = document.cookie.indexOf(";", valStart);
14
        if (valEnd == -1) {
15
          valEnd = document.cookie.length;
16
        }
17
        var val = document.cookie.substring(valStart, valEnd);
18
        return val;
19
      }
20
    }
21
    return 0;
22
  }
23
24
  function writeCookie(cookie, val, expiration)
25
  {
26
    if (val==undefined) return;
0 ignored issues
show
Best Practice introduced by
Comparing val to undefined using the == operator is not safe. Consider using === instead.
Loading history...
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
27
    if (expiration == null) {
0 ignored issues
show
Best Practice introduced by
Comparing expiration to null using the == operator is not safe. Consider using === instead.
Loading history...
28
      var date = new Date();
29
      date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week
30
      expiration = date.toGMTString();
31
    }
32
    document.cookie = cookie_namespace + "_" + cookie + "=" + val + "; expires=" + expiration+"; path=/";
33
  }
34
35
  function resizeWidth()
36
  {
37
    var windowWidth = $(window).width() + "px";
0 ignored issues
show
Unused Code introduced by
The variable windowWidth seems to be never used. Consider removing it.
Loading history...
38
    var sidenavWidth = $(sidenav).outerWidth();
39
    content.css({marginLeft:parseInt(sidenavWidth)+"px"});
40
    writeCookie('width',sidenavWidth-barWidth, null);
41
  }
42
43
  function restoreWidth(navWidth)
44
  {
45
    var windowWidth = $(window).width() + "px";
0 ignored issues
show
Unused Code introduced by
The variable windowWidth seems to be never used. Consider removing it.
Loading history...
46
    content.css({marginLeft:parseInt(navWidth)+barWidth+"px"});
47
    sidenav.css({width:navWidth + "px"});
48
  }
49
50
  function resizeHeight()
51
  {
52
    var headerHeight = header.outerHeight();
53
    var footerHeight = footer.outerHeight();
54
    var windowHeight = $(window).height() - headerHeight - footerHeight;
55
    content.css({height:windowHeight + "px"});
56
    navtree.css({height:windowHeight + "px"});
57
    sidenav.css({height:windowHeight + "px"});
58
    var width=$(window).width();
59
    if (width!=collapsedWidth) {
60
      if (width<desktop_vp && collapsedWidth>=desktop_vp) {
61
        if (!collapsed) {
62
          collapseExpand();
63
        }
64
      } else if (width>desktop_vp && collapsedWidth<desktop_vp) {
65
        if (collapsed) {
66
          collapseExpand();
67
        }
68
      }
69
      collapsedWidth=width;
70
    }
71
  }
72
73
  function collapseExpand()
74
  {
75
    if (sidenav.width()>0) {
76
      restoreWidth(0);
77
      collapsed=true;
78
    }
79
    else {
80
      var width = readCookie('width');
81
      if (width>200 && width<$(window).width()) { restoreWidth(width); } else { restoreWidth(200); }
82
      collapsed=false;
83
    }
84
  }
85
86
  header  = $("#top");
87
  sidenav = $("#side-nav");
88
  content = $("#doc-content");
89
  navtree = $("#nav-tree");
90
  footer  = $("#nav-path");
0 ignored issues
show
Bug introduced by
The variable footer seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.footer.
Loading history...
91
  $(".side-nav-resizable").resizable({resize: function(e, ui) { resizeWidth(); } });
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter ui is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
92
  $(sidenav).resizable({ minWidth: 0 });
93
  $(window).resize(function() { resizeHeight(); });
94
  var device = navigator.userAgent.toLowerCase();
0 ignored issues
show
Bug introduced by
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
95
  var touch_device = device.match(/(iphone|ipod|ipad|android)/);
96
  if (touch_device) { /* wider split bar for touch only devices */
97
    $(sidenav).css({ paddingRight:'20px' });
98
    $('.ui-resizable-e').css({ width:'20px' });
99
    $('#nav-sync').css({ right:'34px' });
100
    barWidth=20;
101
  }
102
  var width = readCookie('width');
103
  if (width) { restoreWidth(width); } else { resizeWidth(); }
104
  resizeHeight();
105
  var url = location.href;
106
  var i=url.indexOf("#");
107
  if (i>=0) window.location.hash=url.substr(i);
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
108
  var _preventDefault = function(evt) { evt.preventDefault(); };
109
  $("#splitbar").bind("dragstart", _preventDefault).bind("selectstart", _preventDefault);
110
  $(".ui-resizable-handle").dblclick(collapseExpand);
111
  $(window).load(resizeHeight);
112
}
113
114
115